home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_02 / 1102075a < prev    next >
Text File  |  1992-12-12  |  6KB  |  201 lines

  1.    /************************************************
  2.    *
  3.    *   peak_threshold_segmentation(...
  4.    *
  5.    *   This function segments an image using 
  6.    *   thresholding.  It uses the histogram peaks 
  7.    *   to find the hi and low values of the 
  8.    *   threshold.
  9.    *
  10.    *   If the segment parameter is 0, you only
  11.    *   threshold the array - you do not segment.
  12.    *
  13.    *************************************************/
  14.  
  15. peak_threshold_segmentation(in_name, out_name,
  16.                             the_image, out_image,
  17.                             il, ie, ll, le,
  18.                             value, segment)
  19.    char   in_name[], out_name[];
  20.    int    il, ie, ll, le, segment;
  21.    short  the_image[ROWS][COLS],
  22.           out_image[ROWS][COLS], value;
  23. {
  24.    int      length, peak1, peak2, width;
  25.    short    hi, low;
  26.    struct   tiff_header_struct image_header;
  27.    unsigned long histogram[GRAY_LEVELS+1];
  28.  
  29.    if(does_not_exist(out_name)){
  30.       printf("\n\nPTS> output file does not exist %s",
  31.               out_name);
  32.       read_tiff_header(in_name, &image_header);
  33.       round_off_image_size(&image_header,
  34.                            &length, &width);
  35.       image_header.image_length = length*ROWS;
  36.       image_header.image_width  = width*COLS;
  37.       create_allocate_tiff_file(out_name, &image_header,
  38.                                 out_image);
  39.    }  /* ends if does_not_exist */
  40.  
  41.    read_tiff_image(in_name, the_image, il, ie, ll, le);
  42.    zero_histogram(histogram);
  43.    calculate_histogram(the_image, histogram);
  44.    smooth_histogram(histogram);
  45.    find_peaks(histogram, &peak1, &peak2);
  46.    peaks_high_low(histogram, peak1, peak2,
  47.                   &hi, &low);
  48.    threshold_image_array(the_image, out_image,
  49.                          hi, low, value);
  50.    if(segment == 1)
  51.       grow(out_image, value);
  52.    write_array_into_tiff_image(out_name, out_image,
  53.                                il, ie, ll, le);
  54.  
  55. }  /* ends peak_threshold_segmentation */
  56.  
  57.    /********************************************
  58.    *
  59.    *   find_peaks(...
  60.    *
  61.    *   This function looks through the histogram
  62.    *   array and finds the two highest peaks.
  63.    *   The peaks must be separated, cannot be
  64.    *   next to each other, by a spacing defined
  65.    *   in cips.h.
  66.    *
  67.    *   The peaks array holds the peak value
  68.    *   in the first place and its location in
  69.    *   the second place.
  70.    *
  71.    *********************************************/
  72.  
  73. find_peaks(histogram, peak1, peak2)
  74.    unsigned long histogram[];
  75.    int *peak1, *peak2;
  76. {
  77.    int distance[PEAKS], peaks[PEAKS][2];
  78.    int i, j=0, max=0, max_place=0;
  79.  
  80.    for(i=0; i<PEAKS; i++){
  81.       distance[i] =  0;
  82.       peaks[i][0] = -1;
  83.       peaks[i][1] = -1;
  84.    }
  85.  
  86.    for(i=0; i<=GRAY_LEVELS; i++){
  87.       max       = histogram[i];
  88.       max_place = i;
  89.       insert_into_peaks(peaks, max, max_place);
  90.    }  /* ends loop over i */
  91.  
  92.    for(i=1; i<PEAKS; i++){
  93.       distance[i] = peaks[0][1] - peaks[i][1];
  94.       if(distance[i] < 0)
  95.          distance[i] = distance[i]*(-1);
  96.    }
  97.  
  98.    *peak1 = peaks[0][1];
  99.    for(i=PEAKS-1; i>0; i--)
  100.       if(distance[i] > PEAK_SPACE) *peak2 = peaks[i][1];
  101.  
  102. }  /* ends find_peaks */
  103.  
  104.    /********************************************
  105.    *
  106.    *   insert_into_peaks(...
  107.    *
  108.    *   This function takes a value and its
  109.    *   place in the histogram and inserts them
  110.    *   into a peaks array.  This helps us rank
  111.    *   the the peaks in the histogram.
  112.    *
  113.    *   The objective is to build a list of
  114.    *   histogram peaks and thier locations.
  115.    *
  116.    *   The peaks array holds the peak value
  117.    *   in the first place and its location in
  118.    *   the second place.
  119.    *
  120.    *********************************************/
  121.  
  122. insert_into_peaks(peaks, max, max_place)
  123.    int max, max_place, peaks[PEAKS][2];
  124. {
  125.    int i, j;
  126.  
  127.       /* first case */
  128.    if(max > peaks[0][0]){
  129.       for(i=PEAKS-1; i>0; i--){
  130.          peaks[i][0] = peaks[i-1][0];
  131.          peaks[i][1] = peaks[i-1][1];
  132.       }
  133.       peaks[0][0] = max;
  134.       peaks[0][1] = max_place;
  135.    }  /* ends if */
  136.  
  137.       /* middle cases */
  138.    for(j=0; j<PEAKS-3; j++){
  139.       if(max < peaks[j][0]  && max > peaks[j+1][0]){
  140.          for(i=PEAKS-1; i>j+1; i--){
  141.             peaks[i][0] = peaks[i-1][0];
  142.             peaks[i][1] = peaks[i-1][1];
  143.          }
  144.          peaks[j+1][0] = max;
  145.          peaks[j+1][1] = max_place;
  146.       }  /* ends if */
  147.    }  /* ends loop over j */
  148.       /* last case */
  149.    if(max < peaks[PEAKS-2][0]  && max > peaks[PEAKS-1][0]){
  150.       peaks[PEAKS-1][0] = max;
  151.       peaks[PEAKS-1][1] = max_place;
  152.    }  /* ends if */
  153.  
  154. }  /* ends insert_into_peaks */
  155.  
  156.    /********************************************
  157.    *
  158.    *   peaks_high_low(...
  159.    *
  160.    *   This function uses the histogram array
  161.    *   and the peaks to find the best high and
  162.    *   low threshold values for the threshold
  163.    *   function.  You want the hi and low values
  164.    *   so that you will threshold the image around
  165.    *   the smaller of the two "humps" in the
  166.    *   histogram.  This is because the smaller
  167.    *   hump represents the objects while the
  168.    *   larger hump represents the background.
  169.    *
  170.    *********************************************/
  171.  
  172. peaks_high_low(histogram, peak1, peak2, hi, low)
  173.    int  peak1, peak2;
  174.    short *hi, *low;
  175.    unsigned long histogram[];
  176. {
  177.    int i, mid_point;
  178.    unsigned long sum1 = 0, sum2 = 0;
  179.  
  180.    if(peak1 > peak2)
  181.       mid_point = ((peak1 - peak2)/2) + peak2;
  182.    if(peak1 < peak2)
  183.       mid_point = ((peak2 - peak1)/2) + peak1;
  184.  
  185.    for(i=0; i<mid_point; i++)
  186.       sum1 = sum1 + histogram[i];
  187.  
  188.    for(i=mid_point; i<=GRAY_LEVELS; i++)
  189.       sum2 = sum2 + histogram[i];
  190.    if(sum1 >= sum2){
  191.       *low = mid_point;
  192.       *hi  = GRAY_LEVELS;
  193.    }
  194.    else{
  195.       *low = 0;
  196.       *hi  = mid_point;
  197.    }
  198.  
  199. }  /* ends peaks_high_low */
  200.  
  201.